package com.mes.basicdata.domain;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import org.hibernate.annotations.BatchSize;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.ForeignKey;
import org.hibernate.annotations.GenericGenerator;
import org.springframework.context.annotation.Lazy;


/**
 * <p>
 * 用来描述工序之间的连接关系，包括与、或、并、可合并和可拆分。
 * </p>
 * 
 * <p>
 * Created on 2017-03-16
 * </p>
 * 
 * @author lcs
 * 
 */
@Entity()
@org.hibernate.annotations.Entity(dynamicInsert=true,dynamicUpdate=true)
@Table(name = "B_ROUTINE_LINKS")
@BatchSize(size=10)
@Lazy(value=true)
@Cache(usage=CacheConcurrencyStrategy.READ_WRITE)
public class OperationLink extends ProcessNodeLink implements Serializable, Comparable {
	
	private static final long serialVersionUID = 1L;

	// 工序链接编号
	@Id
	@GenericGenerator(name = "operationLinkUid", strategy = "assigned")
	@Column(name="UNIQUEID",precision=30, scale=0)
	private String operationLinkUid;
	
	// 链接数量
	@Column(name="LINKQTY",precision=25, scale=6)
	private Float operationLinkQty;
	
	// 工序链接类型
	@Column(name="LINKTYPE")
	private Integer operationLinkType;
	
	// 链接滞后
	@Column(name="LINKLAG",precision=10, scale=0)
	private Integer operationLinkLag;
	
	// 后置工序
	@Column(name="SUCCUID")
	private String succeedOperationUid;
	//many-to-one
	@ForeignKey(name="null")
	@ManyToOne(fetch=FetchType.LAZY)
	@JoinColumn(name="SUCCUID",insertable=false,updatable=false)
	private Operation succeedOperation;
	
	// 前置工序
	@Column(name="PREDUID")
	private String precedeOperationUid;
	//many-to-one
	@ForeignKey(name="null")
	@ManyToOne(fetch=FetchType.LAZY)
	@JoinColumn(name="PREDUID",insertable=false,updatable=false)
	private Operation precedeOperation;
	
	// 工艺
	@Column(name="ROUTING")
	private String routingUid;
	@ForeignKey(name="null")
	@ManyToOne(fetch=FetchType.LAZY)
	@JoinColumn(name="ROUTING",insertable=false,updatable=false)
	private Routing routing;
	
	// 条件uid
	@Column(name="CONDITIONS_UID")
	private String conditionsUid;
	@Column(name="condition_Expr")
	private String conditionExpr;
	@Column(name="condition_expr_default")
	private String conditionExprDefault;

	// 链接名称
	private String operationLinkName;
	// 后置工序编号
	private Integer succeedOperationId;
	// 前置工序编号
	private Integer precedeOperationId;
	private Boolean base;
	private Boolean current;

	
	public OperationLink() {
		super();
	}
	
	@Override
	public String getUuid() {
		// TODO Auto-generated method stub
		return this.getOperationLinkUid();
	}
	@Override
	public void setUuid(String uuid) {
		// TODO Auto-generated method stub
		this.setOperationLinkUid(uuid);
	}

	public String getOperationLinkUid() {
		return operationLinkUid;
	}

	public void setOperationLinkUid(String operationLinkUid) {
		this.operationLinkUid = operationLinkUid;
	}

	public Float getOperationLinkQty() {
		return operationLinkQty;
	}

	public void setOperationLinkQty(Float operationLinkQty) {
		this.operationLinkQty = operationLinkQty;
	}

	public Integer getOperationLinkType() {
		return operationLinkType;
	}

	public void setOperationLinkType(Integer operationLinkType) {
		this.operationLinkType = operationLinkType;
	}

	public Integer getOperationLinkLag() {
		return operationLinkLag;
	}

	public void setOperationLinkLag(Integer operationLinkLag) {
		this.operationLinkLag = operationLinkLag;
	}

	public String getSucceedOperationUid() {
		return succeedOperationUid;
	}

	public void setSucceedOperationUid(String succeedOperationUid) {
		this.succeedOperationUid = succeedOperationUid;
	}

	public Operation getSucceedOperation() {
		return succeedOperation;
	}

	public void setSucceedOperation(Operation succeedOperation) {
		this.succeedOperation = succeedOperation;
	}

	public String getPrecedeOperationUid() {
		return precedeOperationUid;
	}

	public void setPrecedeOperationUid(String precedeOperationUid) {
		this.precedeOperationUid = precedeOperationUid;
	}

	public Operation getPrecedeOperation() {
		return precedeOperation;
	}

	public void setPrecedeOperation(Operation precedeOperation) {
		this.precedeOperation = precedeOperation;
	}

	public String getRoutingUid() {
		return routingUid;
	}

	public void setRoutingUid(String routingUid) {
		this.routingUid = routingUid;
	}

	public Routing getRouting() {
		return routing;
	}

	public void setRouting(Routing routing) {
		this.routing = routing;
	}

	public String getConditionsUid() {
		return conditionsUid;
	}

	public void setConditionsUid(String conditionsUid) {
		this.conditionsUid = conditionsUid;
	}

	public String getConditionExpr() {
		return conditionExpr;
	}

	public void setConditionExpr(String conditionExpr) {
		this.conditionExpr = conditionExpr;
	}

	public String getConditionExprDefault() {
		return conditionExprDefault;
	}

	public void setConditionExprDefault(String conditionExprDefault) {
		this.conditionExprDefault = conditionExprDefault;
	}

	public String getOperationLinkName() {
		return operationLinkName;
	}

	public void setOperationLinkName(String operationLinkName) {
		this.operationLinkName = operationLinkName;
	}

	public Integer getSucceedOperationId() {
		return succeedOperationId;
	}

	public void setSucceedOperationId(Integer succeedOperationId) {
		this.succeedOperationId = succeedOperationId;
	}

	public Integer getPrecedeOperationId() {
		return precedeOperationId;
	}

	public void setPrecedeOperationId(Integer precedeOperationId) {
		this.precedeOperationId = precedeOperationId;
	}

	public Boolean getBase() {
		return base;
	}

	public void setBase(Boolean base) {
		this.base = base;
	}

	public Boolean getCurrent() {
		return current;
	}

	public void setCurrent(Boolean current) {
		this.current = current;
	}



	public int compareTo(Object o) {
		if (!(o instanceof OperationLink)) {
			return 1;
		}
		OperationLink operationLink = (OperationLink) o;
		if (this.getPrecedeOperation() != null
				&& operationLink.getPrecedeOperation() == null) {
			return 1;
		} else if (this.getPrecedeOperation() == null
				&& operationLink.getPrecedeOperation() != null) {
			return -1;
		} else if (this.getPrecedeOperation() == null
				&& operationLink.getPrecedeOperation() != null) {
			return 0;
		} else {
			return this.getPrecedeOperation().compareTo(
					operationLink.getPrecedeOperation());
		}
	}

	
	@Override
	public String toString() {
		return "OPLink:"+getOperationLinkUid()+" "+getPrecedeOperation().toString()+"->"+getSucceedOperation().toString();
	}
	
}
